package com.mastercard.assets.web.struts.actions;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.mastercard.assets.jpa.TblFlujoAutorizacion;
import com.mastercard.assets.jpa.TblRole;
import com.mastercard.assets.util.MCUtil;
import com.mastercard.assets.util.dao.MasterCardDAO;
import com.mastercard.assets.web.struts.forms.CatalogoNuevoItemForm;
import com.mastercard.assets.web.struts.forms.FlujosFForm;
import com.mastercard.assets.web.util.to.UserLogin;

public class FlujosAutAdd
	extends
		Action
{
	public ActionForward execute(
							ActionMapping mapping , 
							ActionForm form ,
							HttpServletRequest request , 
							HttpServletResponse response )
		throws 
			IOException , 
			ServletException
	{
		HttpSession session = request.getSession( ) ;
		
		// en caos de null
		if( session == null )
			return mapping.findForward( "noLogin" ) ;
		
		// toma el userLogin
		UserLogin user = ( UserLogin ) session.getAttribute( "UserLogin" ) ;
		
		// en caso de null
		if( user == null )
			return mapping.findForward( "noLogin" ) ;
		
		CatalogoNuevoItemForm f = ( CatalogoNuevoItemForm ) form ;
                
                int operation = f.getOperation();
                if(operation==1){
                    String nombre = f.getCampo1();
                    int prioridad = f.getCampoInt1();
                    int[] selRoles = f.getCampoIntArr1();
                    int[] ordenRoles = f.getCampoIntArr2();
                    String max = MasterCardDAO.addFlujosAut(nombre,prioridad);
                    if(selRoles != null){
                        for(int i=0;i<selRoles.length;i++){
                            int rol = selRoles[i];
                            int orden = ordenRoles[i];
                            if(ordenRoles[i]>0){
                                MasterCardDAO.addFlujosAutRol(rol,orden,Integer.parseInt(max));
                            }
                        }
                    }
                    user.setNextWorkArea("/WEB-INF/pages/WorkArea/FlujosAutorizacion.jsp");
                    MasterCardDAO.closeEntityManager( ) ;
                    return mapping.findForward( "next" ) ;                    
                }else if(operation==2){
            
                }else if(operation==3){
                    
                }
                
                List< TblRole > Roles =  MasterCardDAO.getAllActiveRoles( );
                request.setAttribute( "Roles" , Roles ) ;
                
                MasterCardDAO.closeEntityManager( ) ;
		user.setNextWorkArea("/WEB-INF/pages/WorkArea/FlujosAutAdd.jsp");
		return mapping.findForward( "success" ) ;
	}
	
}